package com.app.twth.ui.search;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.app.twth.app.Constant;
import com.app.twth.base.BaseAndroidViewModel;
import com.app.twth.bean.BaseResponseEntity;
import com.app.twth.bean.LoadingBean;
import com.app.twth.net.JsonCallBack;
import com.lzy.okgo.model.Response;

import java.util.List;


/**
 * ****************************************************************
 * 文件名称: LoginViewModel
 * 文件描述:
 * 注意事项:
 * 版权声明:
 * ****************************************************************
 */
public class SearchViewModel extends BaseAndroidViewModel<SearchDataModel> {

    public MutableLiveData<SearchContainer> containerLiveData = new MutableLiveData<>();
    public MutableLiveData<SearchTrk> trkLiveData = new MutableLiveData<>();
    public MutableLiveData<SearchIBLpns> iBLpnLiveDatas = new MutableLiveData<>();
    public MutableLiveData<List<SearchIBLpn>> iBLpnLiveData = new MutableLiveData<>();
    public MutableLiveData<List<SearchOBLpn>> oBLpnLiveData = new MutableLiveData<>();

    public SearchViewModel(@NonNull Application application) {
        super(application);
    }

    public void searchContainer(String containerNo) {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.searchContainer(containerNo, new JsonCallBack<BaseResponseEntity<SearchContainer>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<SearchContainer>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<SearchContainer> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    SearchContainer data = baseResponseEntity.data;
                    if (data == null) {
                        return;
                    }
                    containerLiveData.setValue(data);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void searchIbLpn(String ibLpn) {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.searchIbLpn(ibLpn, new JsonCallBack<BaseResponseEntity<SearchIBLpns>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<SearchIBLpns>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<SearchIBLpns> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    SearchIBLpns data = baseResponseEntity.data;
                    if (data == null) {
                        return;
                    }
                    iBLpnLiveDatas.setValue(data);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void searchLocation(String locationCode) {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.searchLocation(locationCode, new JsonCallBack<BaseResponseEntity<List<SearchIBLpn>>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<List<SearchIBLpn>>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<List<SearchIBLpn>> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    List<SearchIBLpn> data = baseResponseEntity.data;
                    if (data == null) {
                        return;
                    }
                    iBLpnLiveData.setValue(data);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void searchObLpn(String obLpn) {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.searchObLpn(obLpn, new JsonCallBack<BaseResponseEntity<List<SearchOBLpn>>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<List<SearchOBLpn>>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<List<SearchOBLpn>> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    List<SearchOBLpn> data = baseResponseEntity.data;
                    if (data == null) {
                        return;
                    }
                    oBLpnLiveData.setValue(data);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void searchSku(String sku) {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.searchSku(sku, new JsonCallBack<BaseResponseEntity<List<SearchIBLpn>>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<List<SearchIBLpn>>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<List<SearchIBLpn>> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    List<SearchIBLpn> data = baseResponseEntity.data;
                    if (data == null) {
                        return;
                    }
                    iBLpnLiveData.setValue(data);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void searchTrk(String trk) {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.searchTrk(trk, new JsonCallBack<BaseResponseEntity<SearchTrk>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<SearchTrk>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<SearchTrk> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    SearchTrk data = baseResponseEntity.data;
                    if (data == null) {
                        return;
                    }
                    trkLiveData.setValue(data);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }


}
